package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.EmptyUtil;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Autowired
    AdChannelMapper adChannelMapper;
    /**
     * @param dto
     * @return 数据列表
     */
    @Override
    public ResponseResult<List<AdChannel>> findByNameAndPage(ChannelDto dto) {
        //1.判断参数是否存在
        if(EmptyUtil.isNullOrEmpty(dto)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        if(dto.getName() != null){
            wrapper.like(AdChannel::getName,dto.getName());
        }
        dto.checkParam();
        Page<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());


        //2.执行业务
        IPage<AdChannel> pageResult = page(page, wrapper);


        //3.解析并封装结果返回
        ResponseResult<List<AdChannel>> result = new PageResponseResult(dto.getPage(), dto.getSize(), (int)pageResult.getTotal());
        result.setData(pageResult.getRecords());
        return result;
    }
    /**
     * @param channel
     * @return 是否插入成功
     */
    @Override
    public ResponseResult insert(AdChannel channel) {
        //1判断参数是否可用
        if(EmptyUtil.isNullOrEmpty(channel)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        channel.setCreatedTime(new Date());
        channel.setIsDefault(false);
        save(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改数据
     * @param channel
     * @return 是否修改
     */
    @Override
    public ResponseResult update(AdChannel channel) {
        //1.判断参数是否存在
        if(EmptyUtil.isNullOrEmpty(channel)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel adChannel = getById(channel.getId());
        if(EmptyUtil.isNullOrEmpty(adChannel)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        updateById(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel channel = getById(id);
        if(EmptyUtil.isNullOrEmpty(channel)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_IS_OPEN);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
